package com.hzqy.service.produceInfoJs;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.FileUtil;
import com.hzqy.commons.utils.HttpConectUtil;
import com.hzqy.persistence.dao.commons.CommonsDao;
import com.hzqy.service.commons.OrgModelFilterService;
import com.hzqy.service.hqjc.HqjcServiceBean;
import com.hzqy.service.itf.YxtItfServiceBean;
import com.hzqy.web.vo.CmCatalogVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsOrgVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CountyEducationStyleExpandServiceBean{
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private YxtItfServiceBean yxtItfService;
	@Resource
	private HqjcServiceBean hqjcServiceBean;
	@Resource
	private CommonsDao dataDao;
	public Object executeWrite(HttpServletRequest req, HttpSession session, String path,String serverIdStr) {
		LOG.debug("path="+path);
		LOG.debug("serverIdStr="+serverIdStr);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("code","-1");
		map.put("message","更新失败");
		if(path==null||"".equals(path)){
			map.put("write_log","路径不正确");
			return map;
		}
		if(!path.endsWith("/")&&!path.endsWith("\\"))
			path=path+File.separator;
		if(serverIdStr==null||"".equals(serverIdStr)){
			map.put("write_log","serverIdStr不正确");
			return map;
		}
		String write_log="";
		try{
			OrgModelFilterService filterService = OrgModelFilterService.getInstance(req.getRequestURI(), session);//通过uri获取接下去所有逻辑操作的业务对象
			String[] serverIdArr=serverIdStr.split(",");
			int count=serverIdArr.length;
			int countSuccess=0;
			for (int i = 0; i < serverIdArr.length; i++) {
				String serverId=serverIdArr[i];
				String fileName=serverId+".js";
				try {
					PmsOrgVo orgVo=yxtItfService.selectContentOrgList(serverId, filterService.getCatalogidOfServerid()); //这里定义业务账号必须绑定目录
					CmContentVo contentVo = new CmContentVo();
					contentVo.setF_cc_id(orgVo.getF_po_model());
					contentVo=hqjcServiceBean.selectContent(contentVo);
					if(contentVo != null){//获取企业绑定模板路径
						String modelPath=contentVo.getF_cc_desc().replaceAll("<([^>]*)>","");
						orgVo.setF_po_model_url(modelPath);
					}
					String f_po_model_url=orgVo.getF_po_model_url();
					String f_catalog_id=this.parseUrl(f_po_model_url);
					LOG.debug(serverId+"模板地址="+f_po_model_url);
					String areaid="";
					if(serverId!=null && serverId.trim().length()>0){
						areaid = dataDao.selectAreaIdByServerId(serverId);
					}
					LOG.debug(serverId+"区域="+areaid);
					LOG.debug(serverId+"首页分类id="+f_catalog_id);
					List<CmContentVo> infos_logo=catalog_list("1337", 0, 1, "2", areaid,orgVo);
					List<Map<String,Object>> infos_logo_map=new ArrayList<Map<String,Object>>();
					if(infos_logo!=null&&infos_logo.size()>0){
						Map<String,Object> mapLogo=new HashMap<String,Object>();
						mapLogo.put("f_cc_id",infos_logo.get(0).getF_cc_id());
						mapLogo.put("f_cc_picurl",infos_logo.get(0).getF_cc_picurl());
						mapLogo.put("f_cc_name",infos_logo.get(0).getF_cc_name());
						infos_logo_map.add(mapLogo);
					}
					LOG.debug("infos_logo="+infos_logo);
					List<CmCatalogVo> infos_sy=new ArrayList<CmCatalogVo>();
					List<Map<String,Object>> infos_sy_map=new ArrayList<Map<String,Object>>();
					Map<String,Object> infos_km=new HashMap<String, Object>();
					Map<String,Object> infos_list=new HashMap<String, Object>();
					if("".equals(f_catalog_id)){
						write_log+=serverId+"首页分类id空"+"<br/>";
					}else{
						infos_sy=catalog_catalogInfoListBycatalogId(f_catalog_id,0, 1000);
						LOG.debug("infos_sy="+infos_sy);
						for (int j = 0; j < infos_sy.size(); j++) {
							CmCatalogVo infos_syi=infos_sy.get(j);
							Map<String,Object> mapSy=new HashMap<String,Object>();
							mapSy.put("f_catalog_id",infos_syi.getF_catalog_id());
							mapSy.put("f_catalog_name",infos_syi.getF_catalog_name());
							mapSy.put("f_catalog_desc",infos_syi.getF_catalog_desc());
							infos_sy_map.add(mapSy);

							List<CmCatalogVo> infos_km_one=catalog_catalogInfoListBycatalogId(""+infos_syi.getF_catalog_id(),0, 1000);
							List<Map<String,Object>> infos_km_one_map=new ArrayList<Map<String,Object>>();
							for (int k = 0; k < infos_km_one.size(); k++) {
								CmCatalogVo infos_km_one_i=infos_km_one.get(k);
								Map<String,Object> map_km_one=new HashMap<String,Object>();
								map_km_one.put("f_catalog_id",infos_km_one_i.getF_catalog_id());
								map_km_one.put("f_catalog_name",infos_km_one_i.getF_catalog_name());
								map_km_one.put("f_catalog_desc",infos_km_one_i.getF_catalog_desc());
								infos_km_one_map.add(map_km_one);

								List<CmContentVo> infos_list_one=catalog_list(""+infos_km_one_i.getF_catalog_id(), 0, 1000, "2", areaid,orgVo);
								List<Map<String,Object>> infos_list_one_map=new ArrayList<Map<String,Object>>();
								for (int l = 0; l < infos_list_one.size(); l++) {
									CmContentVo vo=infos_list_one.get(l);
									Map<String,Object> map_list_one=new HashMap<String,Object>();
									map_list_one.put("f_cc_id",vo.getF_cc_id());
									map_list_one.put("f_cc_code",vo.getF_cc_code());
									map_list_one.put("f_cc_name",vo.getF_cc_name());
									map_list_one.put("f_catalog_id",vo.getF_catalog_id());
									map_list_one.put("f_cc_obligate1",vo.getF_cc_obligate1());
									map_list_one.put("f_cc_obligate2",vo.getF_cc_obligate2());
									map_list_one.put("f_cc_obligate3",vo.getF_cc_obligate3());
									infos_list_one_map.add(map_list_one);
								}
								infos_list.put("_"+infos_km_one_i.getF_catalog_id(),infos_list_one_map);
							}
							infos_km.put("_"+infos_syi.getF_catalog_id(),infos_km_one_map);
						}
					}
					JSONObject json=new JSONObject();
					json.put("infos_logo",infos_logo_map);
					json.put("infos_sy",infos_sy_map);
					json.put("infos_km",infos_km);
					json.put("infos_list",infos_list);
//					String path=System.getProperty("user.dir")+"/school_hd/county_education_style_expand_js/"+fileName;
//					String path="/opt/MoNi/tomcat-82/webapps/web/school_hd/county_education_style_expand_js/"+fileName;
					String path_=path+fileName;
					FileUtil.inputFileString(path_,"var infos_all="+json.toString());
					write_log+="生成"+fileName+"成功-";
					countSuccess++;
				}catch (Exception e){
					LOG.error("生成"+fileName+"异常",e);
					write_log+="<br/>生成"+fileName+"异常-"+e+"<br/>";
				}
			}
			map.put("code","0");
			map.put("message","共"+count+"个业务账号,更新成功"+countSuccess+"个");
		}catch (Exception e){
			LOG.error("生成js文件异常",e);
			map.put("Exception",e);
		}
		map.put("write_log",write_log);
		return map;
	}


	public Object updateInfoJs(String url, String path,String serverIdStr){
		LOG.debug("url="+url);
		LOG.debug("path="+path);
		LOG.debug("serverIdStr="+serverIdStr);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("code","-1");
		if(url==null||"".equals(url)){
			map.put("message","访问路径不正确");
			return map;
		}
		if(path==null||"".equals(path)){
			map.put("message","物理路径不正确");
			return map;
		}
		if(serverIdStr==null||"".equals(serverIdStr)){
			map.put("message","serverIdStr不正确");
			return map;
		}
		String sendUrl="";
		String ret="";
		try{
			url="http://"+url+"/web/school_hd/produceInfoJs/countyEducationStyle.do";
			String epg_info="<server_ip>220.191.139.79</server_ip><group_name>10013</group_name><group_path></group_path><oss_user_id>1570qzkhjyj</oss_user_id><page_url>http://220.191.139.79:8080/EPG/jsp/ZJTELECOMHotelHD/en/vis_to_page.jsp</page_url><partner>HWGDHotelHD</partner>";
			sendUrl=url+"?path="+ URLEncoder.encode(path, "utf-8")+"&serveridStr="+URLEncoder.encode(serverIdStr, "utf-8")
					+"&epg_info="+ URLEncoder.encode(epg_info, "utf-8");
			ret=HttpConectUtil.sendPost(sendUrl,20*60*1000,60*1000);
			map.put("code","0");
			map.put("ret",ret);
		}catch (Exception e){
			LOG.error("url请求失败:"+sendUrl,e);
			map.put("Exception","url请求失败:"+sendUrl+"e");
		}
		return map;
	}
	public String parseUrl(String url){
		String f_catalog_id="";
		String [] arr ;
		if(url!=null&url.indexOf("?")!=-1&&url.length()-1>url.indexOf("?")){
			url=url.substring(url.indexOf("?")+1);
			arr=url.split("&");
			for (int i = 0; i < arr.length; i++) {
				String [] arri=arr[i].split("=");
				if (arri.length==2&&"gradeId".equals(arri[0])){
					f_catalog_id=arri[1];
					break;
				}
			}
		}
		return f_catalog_id;
	}
	public List<CmContentVo>  catalog_list(String f_catalog_id, int pageNo, int pageSize, String f_cp_cprid, String f_sa_id,PmsOrgVo orgVo) {
		CmContentVo contentVo=new CmContentVo();
		contentVo.setF_cc_interfaceType(1);
		contentVo.setF_sa_id(f_sa_id);
		int f_po_id=0;
		if(orgVo!=null && contentVo.getF_is_public()==0){
			f_po_id=orgVo.getF_po_id();
		}
		List<CmContentVo> contentList = hqjcServiceBean.selectContentListByCatalogId(Integer.parseInt(f_catalog_id),""+f_po_id, pageNo, pageSize,f_cp_cprid,contentVo);
		return contentList;
	}
	public List<CmCatalogVo> catalog_catalogInfoListBycatalogId(String f_catalog_id,int pageNo,int pageSize) {
		int f_po_id=0;
		List<CmCatalogVo> catalogList = hqjcServiceBean.selectCatalogInfoListByParentCatalogId(Integer.parseInt(f_catalog_id),f_po_id, pageNo, pageSize);
		return catalogList;
	}

	public static void main(String[] args) {
		String path=System.getProperty("user.dir")+"/school_hd/county_education_style_expand_js/";
		System.out.println(path);
	}
}
